package com.fr.data.impl.sap;

import com.fr.file.XMLFileManager;
import com.fr.general.FRLogger;
import com.fr.general.GeneralContext;
import com.fr.stable.EnvChangedListener;
import com.fr.stable.ListMap;
import com.fr.stable.xml.*;

import java.io.InputStream;
import java.util.Iterator;

public class SAPTransferManager extends XMLFileManager implements XMLable {
    private static SAPTransferManager manager = null;

    static {
        GeneralContext.addEnvChangedListener(new EnvChangedListener() {
            public void envChanged() {
                SAPTransferManager.envChanged();
            }
        });
    }

    public static final String XML_TAG = "SAPTransferManager";

    private ListMap transferMap = new ListMap();

    public static SAPTransferManager getInstance() {
        if (manager == null) {
            manager = new SAPTransferManager();
            manager.readXMLFile();
        }

        return manager;
    }


    private synchronized static void envChanged() {
        manager = null;
    }

    private SAPTransferManager() {

    }

    public String fileName() {
        return "saptransfer.xml";
    }

    public synchronized void readFromInputStream(InputStream input) throws Exception {
        SAPTransferManager m = new SAPTransferManager();
        XMLTools.readInputStreamXML(m, input);
        manager = m;

        GeneralContext.getEnvProvider().writeResource(manager);
    }

    public Iterator getSAPTransferNameIterator() {
        return this.transferMap.keySet().iterator();
    }

    private SAPTransfer getStoredSAPTransfer(String name) {
        return (SAPTransfer) this.transferMap.get(name);
    }

    public SAPTransfer getSAPTransfer(String name) {
        SAPTransfer transfer = getStoredSAPTransfer(name);
        try {
            return transfer == null ? null : (SAPTransfer) transfer.clone();
        } catch (CloneNotSupportedException e) {
            FRLogger.getLogger().error(e.getMessage(), e);
            return null;
        }
    }

    public void putSAPTransfer(String name, SAPTransfer transfer) {
        if (transfer == null) {
            this.transferMap.remove(name);
        }

        this.transferMap.put(name, transfer);
    }

    public boolean renameSAPTransfer(String oldName, String newName) {
        if (!transferMap.containsKey(oldName) || transferMap.containsKey(newName)) {
            return false;
        }

        int idx = transferMap.indexOf(oldName);
        transferMap.put(idx, newName, transferMap.get(oldName));

        return true;
    }

    public void removeWidgetConfig(String name) {
        this.transferMap.remove(name);
    }

    public void clearAllSAPTransfer() {
        this.transferMap.clear();
    }

    public void readXML(XMLableReader reader) {
        if (reader.isAttr()) {
            this.clearAllSAPTransfer();
        }

        if (reader.isChildNode()) {
            String tagName = reader.getTagName();
            if ("SAPTransferElement".equals(tagName)) {
                final String name = reader.getAttrAsString("name", null);
                if (name == null) {
                    return;
                }

                reader.readXMLObject(new XMLReadable() {
                    public void readXML(XMLableReader reader) {
                        if (reader.isChildNode() && reader.getTagName().equals(SAPTransfer.XML_TAG)) {
                            SAPTransfer transfer = new SAPTransfer();
                            reader.readXMLObject(transfer);
                            SAPTransferManager.this.putSAPTransfer(name, transfer);
                        }
                    }
                });
            }
        }
    }

    public void writeXML(XMLPrintWriter writer) {
        writer.startTAG(XML_TAG);
        Iterator it = this.getSAPTransferNameIterator();
        while (it.hasNext()) {
            String name = (String) it.next();

            SAPTransfer transfer = this.getStoredSAPTransfer(name);
            if (transfer == null) {
                continue;
            }

            writer.startTAG("SAPTransferElement");
            if (name != null) {
                writer.attr("name", name);
            }

            transfer.writeXML(writer);

            writer.end();
        }

        writer.end();
    }

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
